﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Collections;

namespace FOBVT.Web.FOBVT2011.cn
{
    public partial class payment_success : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LabelOrderIDInfo.Text = Request["oid"];
                Double amount =  Convert.ToDouble(GetRegistrationFeeInfo());
                
                this.LabUSValueInfo.Text = Convert.ToString(amount);
                if (FOBVT.Web.MainClass.SessionState.CurrentUser.CustomerID == null)
                {
                    System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\..\\DB\\FOBVTweb.mdb"));
                    FOBVT.Web.MainClass.GetUserInfo user = new FOBVT.Web.MainClass.GetUserInfo();
                    user.Conn = Conn;
                    FOBVT.Web.MainClass.User userInfo = user.GetUserInfoForPDF(Request["customerId"]);
                    FOBVT.Web.MainClass.SessionState.CurrentUser = userInfo;
                }

          }
        }

        //private string GetFeeRateInfo()
        //{
        //    string getFeeRate = "0";
        //    System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\..\\DB\\FOBVTweb.mdb"));
        //    FOBVT.Web.MainClass.DBClass DBClassTmp = new FOBVT.Web.MainClass.DBClass();
        //    string sqlinfo = "SELECT  Rate FROM  RateInfo";
        //    System.Data.DataSet ds = new System.Data.DataSet();
        //    ds = DBClassTmp.GetDataSet(sqlinfo, Conn);
        //    if (ds != null && ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        //    {
        //        getFeeRate = ds.Tables[0].Rows[0]["Rate"].ToString();
        //    }
        //    return getFeeRate;
        //}

        private string GetRegistrationFeeInfo()
        {
            string getRegistrationFee = "0";
            System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\..\\DB\\FOBVTweb.mdb"));
            FOBVT.Web.MainClass.DBClass DBClassTmp = new FOBVT.Web.MainClass.DBClass();
            string sqlinfo = "SELECT TOP 1 CustomerInfo.CustomerID, CustomerInfo.MemberShipID, RegistrationFee.RegistrationFee as RegistrationFee, ";
            sqlinfo += " RegistrationFee.RegistrationDate FROM  (CustomerInfo INNER JOIN ";
            sqlinfo += " RegistrationFee ON CustomerInfo.MemberShipID = RegistrationFee.MembershipID)";
            sqlinfo += " WHERE (RegistrationFee.RegistrationDate > #{0}#) AND (CustomerInfo.CustomerID = '{1}')";
            sqlinfo += " ORDER BY RegistrationFee.RegistrationDate";
            sqlinfo = string.Format(sqlinfo, System.DateTime.Now.ToShortDateString(), Request["customerId"]);

            System.Data.DataSet ds = new System.Data.DataSet();
            ds = DBClassTmp.GetDataSet(sqlinfo, Conn);
            if (ds != null && ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                getRegistrationFee = ds.Tables[0].Rows[0]["RegistrationFee"].ToString();
            }
            return getRegistrationFee;
        }

        private void UpdateInfo()
        {
            System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\..\\DB\\FOBVTweb.mdb"));
            FOBVT.Web.MainClass.DBClass DBClassTmp = new FOBVT.Web.MainClass.DBClass();
            ArrayList sql = new ArrayList();
            sql.Clear();
            sql.Add("UPDATE  CustomerInfo SET StatusID='5',Status='CreatePDF',CreatPDFTime=#" + System.DateTime.Now.ToString() +
              "# WHERE (CustomerID ='" + FOBVT.Web.MainClass.SessionState.CurrentUser.CustomerID + "')");

            if (!DBClassTmp.ExcuteSQL(sql, Conn))
            {
               

            }
        }

        protected void BtnCreatePDF_Click(object sender, EventArgs e)
        {
            if (FOBVT.Web.MainClass.SessionState.CurrentUser.State ==4)
            {
                UpdateInfo();
            }
            ShowPDFInfo();
        }

        private string GetEnglishFormatDateInfo(DateTime inputStr)
        {
            string outputStr = "";
            if (inputStr != null)
            {
                System.Globalization.CultureInfo c = System.Globalization.CultureInfo.CreateSpecificCulture("en");
                outputStr = inputStr.ToString("D", c.DateTimeFormat);
                outputStr = outputStr.Substring(outputStr.IndexOf(',') + 1);
            }
            return outputStr;
        }

        private void ShowPDFInfo()
        {
            //定义一个Document，并设置页面大小为A4，竖向
            iTextSharp.text.Document doc = new Document(PageSize.A4);
            try
            {
                //写实例
                string fileName = Request.PhysicalApplicationPath + @"PDF\" + FOBVT.Web.MainClass.SessionState.CurrentUser.CustomerID + ".pdf";
                string imagePath = Path.Combine(Request.PhysicalApplicationPath, @"images\Name.jpg");
                PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create));


                //打开document
                doc.Open();
                //载入字体
                BaseFont.AddToResourceSearch("iTextAsian.dll");
                BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");


                //"UniGB-UCS2-H" "UniGB-UCS2-V"是简体中文，分别表示横向字 和 纵向字
                //"STSong-Light"是字体名称
                //BaseFont baseFT = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                //baseFT.setFontAndSize(bf, 10);
                Font fontBase = new Font(Font.TIMES_ROMAN, 10, Font.NORMAL);
                Font fontBold = new Font(Font.TIMES_ROMAN, 10, Font.BOLD);
                Font fontBaseBlue = new Font(Font.TIMES_ROMAN, 10, Font.NORMAL, Color.BLUE);
                Font fontOther = new Font(Font.TIMES_ROMAN, 9, Font.NORMAL);

                Anchor anchor = new Anchor("NERC-DTV www.nercdtv.org", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, Font.UNDERLINE, new Color(0, 0, 255)));
                anchor.Reference = "http://www.nercdtv.org";
                anchor.Name = "NERC-DTV www.nercdtv.org";
                Anchor anchor2 = new Anchor("http://www.fmprc.gov.cn/eng/wjb/zwjg/2490/", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, Font.UNDERLINE, new Color(0, 0, 255)));
                anchor2.Reference = "http://www.fmprc.gov.cn/eng/wjb/zwjg/2490/";
                anchor2.Name = "http://www.fmprc.gov.cn/eng/wjb/zwjg/2490/";
                Anchor anchor3 = new Anchor("http://www.FOBTV.org", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, Font.UNDERLINE, new Color(0, 0, 255)));
                anchor3.Reference = "http://www.FOBTV.org";
                anchor3.Name = "http://www.FOBTV.org";

                DateTime creatPDFTime = DateTime.Now;
                if (FOBVT.Web.MainClass.SessionState.CurrentUser.State != 5)
                {
                    creatPDFTime = FOBVT.Web.MainClass.SessionState.CurrentUser.CreatPDFTime;
                }
                doc.Add(new Paragraph(GetEnglishFormatDateInfo(creatPDFTime), fontBase));
                doc.Add(new Paragraph(string.Format("{0} {1},{2}", FOBVT.Web.MainClass.SessionState.CurrentUser.FirstName, FOBVT.Web.MainClass.SessionState.CurrentUser.LastName, FOBVT.Web.MainClass.SessionState.CurrentUser.BusinessTitle), fontBase));
                doc.Add(new Paragraph(FOBVT.Web.MainClass.SessionState.CurrentUser.FullCompanyName, fontBase));
                doc.Add(new Paragraph(string.Format("{0},{1}", FOBVT.Web.MainClass.SessionState.CurrentUser.Address, FOBVT.Web.MainClass.SessionState.CurrentUser.PostalCode), fontBase));
                doc.Add(new Paragraph(string.Format("Passport: {0}", FOBVT.Web.MainClass.SessionState.CurrentUser.Passport), fontBase));
                string dateinfo = GetEnglishFormatDateInfo(FOBVT.Web.MainClass.SessionState.CurrentUser.Birthday.Date);
                doc.Add(new Paragraph(string.Format("Birth Date: {0}", dateinfo), fontBase));
                doc.Add(new Paragraph(" ", fontBase));
                doc.Add(new Paragraph(" ", fontBase));
                doc.Add(new Paragraph(" ", fontBase));
                doc.Add(new Paragraph(string.Format("Dear {0} {1} {2}:", FOBVT.Web.MainClass.SessionState.CurrentUser.Prefix, FOBVT.Web.MainClass.SessionState.CurrentUser.FirstName, FOBVT.Web.MainClass.SessionState.CurrentUser.LastName), fontBold));
                doc.Add(new Paragraph("Thank you for your request for a letter of invitation to participate in the FOBTV2011 Summit in Shanghai, China: Conference 10-11, November 2011.", fontBase));
                doc.Add(new Paragraph(" ", fontBase));
                doc.Add(new Paragraph("Future of Broadcast Television (FOBTV) will be held to foster global collaboration that may lead to the development of common strategies for the future of terrestrial broadcasting and to exchange technical advances in the rapidly converging areas of terrestrial broadcasting, consumer electronics and networking technologies. Industry technology leaders are to be invited to work together to share their visions of the future of broadcasting. Bringing together the collective perspectives of experts worldwide should benefit industry leaders as they identify their optimum national path for terrestrial broadcasting. ", fontBase));
                doc.Add(new Paragraph(" ", fontBase));
                doc.Add(new Paragraph("FOBTV 2011  Host Organization:", fontBold));


                Paragraph myParagraph1 = new Paragraph("National Engineering Research Center of Digital Television (", fontBase);
                myParagraph1.Add(anchor);
                myParagraph1.Add(new Chunk(")", fontBase));
                doc.Add(myParagraph1);


                doc.Add(new Paragraph(" ", fontBase));
                doc.Add(new Paragraph("FOBTV 2011 Date and Venue: ", fontBold));
                doc.Add(new Paragraph("Date: 10-11th Nov 2011", fontBase));
                doc.Add(new Paragraph(string.Format("{0}: {1}", "Venue", "Shanghai International Convention Center"), fontBase));
                doc.Add(new Paragraph("Add: 2727, Riverside Avenue, Pudong, Shanghai, China", fontBase));
                doc.Add(new Paragraph(" ", fontBase));
                doc.Add(new Paragraph("It is important to note that visiting China may require you to obtain a temporary visa. We strongly urge you to contact the visa section of the China Embassy/Consulate nearest you to determine how and when to apply for a visa. ", fontBase));


                Paragraph myParagraph2 = new Paragraph("You can visit the website: ", fontBase);
                myParagraph2.Add(anchor2);
                myParagraph2.Add(new Chunk(" or visit our website: ", fontBase));
                myParagraph2.Add(anchor3);
                myParagraph2.Add(new Chunk(" for detailed information about procedures, types of visas, and the time required for processing.", fontBase));
                doc.Add(myParagraph2);


                doc.Add(new Paragraph(" ", fontBase));
                doc.Add(new Paragraph("Sincerely,", fontBase));
                iTextSharp.text.Image jpeg = iTextSharp.text.Image.GetInstance(imagePath);
                jpeg.Alignment = Element.ALIGN_LEFT;
                doc.Add(jpeg);

                doc.Add(new Paragraph(" ", fontBase));
                doc.Add(new Paragraph("Mr.Pingjian Xia ", fontBase));
                doc.Add(new Paragraph("President", fontBase));
                doc.Add(new Paragraph("National Engineering Research Center of Digital Television", fontBase));
                doc.Add(new Paragraph("Tel:86-21-60897266*5015", fontBase));
                doc.Add(new Paragraph("E-mail: pj.xia@nercdtv.org", fontBase));


                //关闭document
                doc.Close();
                Response.Redirect("/PDF/" + FOBVT.Web.MainClass.SessionState.CurrentUser.CustomerID + ".pdf");
                //Response.Write("<script>window.open('../PDF/" + this.lblUserID.Text + ".pdf','_blank')</script>");
            }
            catch (DocumentException de)
            {
            }
            catch (IOException io)
            {
            }

        }

        protected void Logout_Click(object sender, EventArgs e)
        {

        }
    }
}